/*
 * BestFit.h
 *
 *  Created on: Apr 16, 2013
 *      Author: erik
 */

#pragma once
#ifndef BESTFIT_H_
#define BESTFIT_H_ 2.1
#ident	"@(#)FirstFit.h	2.1	AKK	20120721"

#include <iostream>
using namespace std;
#include "Fitter.h"
#include "Area.h"

/// @class BestFit
class	BestFit : public Fitter
{
public:
	explicit	// see: http://en.cppreference.com/w/cpp/language/explicit
	/// @param cflag	initial status of check-mode
	/// @param type		name of this algorithm (default=FirstFit)
	BestFit(bool cflag, const char *type = "BestFit (lazy)") : Fitter(cflag, type) {}

	/// Ask for an area of at least 'wanted' units.
	/// @returns	An area or 0 if not enough freespace available
	virtual  Area	*alloc(int wanted);	// application asks for space

	/// The application returns an area to freespace.
	/// @param ap	The area returned to free space
	virtual  void	free(Area *ap);

	void printAreas();

	std::list<Area *>  getAreas()
	{
		return  this->areas;
	}

protected:

	/// This is the actual function that searches for space.
	/// @returns	An area or 0 if not enough freespace available
	virtual	 Area 	*searcher(int);

	/// This function is called when the searcher can not find space.
	/// It tries to reclaim fragmented space by merging adjacent free areas.
	/// @returns true if free areas could be merged, false if no adjacent areas exist
	virtual	 bool	  reclaim();

	// Insert area in to area list
	virtual	 void 	  insertArea(Area  *newArea);

};

#endif /* BESTFIT_H_ */
